<!DOCTYPE html>
<html lang="en">
<title>权限中心-修改用户</title>
<#import "/layout/layout.html" as body/>
<@body.page>

<link href="${baseUrl}css/plugins/awesome-bootstrap-checkbox/awesome-bootstrap-checkbox.css" rel="stylesheet">
<link href="${baseUrl}css/plugins/iCheck/custom.css" rel="stylesheet">
<style>
    .is-checkbox {
        min-height: 200px
    }

    .is-checkbox .checkbox {
        width: 100%
    }
</style>

<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5 class="pull-left">新增用户</h5>
                </div>
                <div class="ibox-content">
                    <form method="post" name="user_add_form" class="form-horizontal">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">用户姓名</label>
                            <div class="col-sm-10"><input type="text" id="userName" value="${sysUser.userName!}" name="userName" class="form-control"></div>
                            <input type="hidden" name="menusId" id="menusId">
                            <input type="hidden" name="id" value="${sysUser.id}">
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">手机号码</label>
                            <div class="col-sm-10"><input type="text" id="mobile" value="${sysUser.mobile!}" name="mobile" class="form-control"></div>
                        </div>
                        <!--超级管理员才可修改其他用户的登陆密码-->
                            <#if Session.sys_user?? && Session.sys_user.roleId?? && Session.sys_user.roleId ==2>
                                <div class="hr-line-dashed"></div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">用户密码</label>
                                    <div class="col-sm-10"><input type="text" id="userPwd" name="userPwd" class="form-control"></div>
                                </div>
                            </#if>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group"><label class="col-sm-2 control-label">用户类型</label>
                            <div class="col-sm-10">
                                <select id="roleId" class="form-control m-b" name="roleId">
                                    <option value="">请选择</option>
                                    <#list role_list as item>
                                        <#if item.id == sysUser.roleId>
                                            <option selected="selected" value="${item.id}">
                                                ${item.roleName}
                                            </option>
                                            <#else>
                                                <option value="${item.id}">
                                                    ${item.roleName}
                                                </option>
                                        </#if>
                                    </#list>
                                </select>
                            </div>
                        </div>
                        <div id="private_show" style="">
                        	<div class="hr-line-dashed"></div>
	                        <div class="form-group" id="store_div" ><label class="col-sm-2 control-label">所属门店</label>
	                            <div class="col-sm-10">
	                                <select id="storeId" class="form-control m-b" name="storeId">
	                                    <option value="0">请选择</option>
	                                    <#list storeList as item>
	                                        <#if item.id == sysUser.storeId>
                                            <option selected="selected" value="${item.id}">
                                                ${item.storeName}
                                            </option>
                                            <#else>
                                                <option value="${item.id}">
                                                    ${item.storeName}
                                                </option>
                                        </#if>
	                                    </#list>
	                                </select>
	                            </div>
	                        </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group"><label class="col-sm-2 control-label">所属角色</label>
                            <div class="col-sm-10">
                                <div class="row">
                                    <#list menu_list as item>
                                        <#if item.parentNo == 0>
                                            <div class="col-sm-3 is-checkbox">
                                                <div class="checkbox checkbox-primary">
                                                    <input id="checkbox-${item.id}" ${item.isChecked} value="${item.id}" type="checkbox" class="is-checkedAll">
                                                    <label for="checkbox-${item.id}">${item.menuName}</label>
                                                </div>
                                                <div style="margin-left: 20px">
                                                    <#list menu_list as item_detail>
                                                        <#if item.id == item_detail.parentNo>
                                                            <div class="checkbox checkbox-primary">
                                                                <input id="checkbox-${item_detail.id}" ${item_detail.isChecked} value="${item_detail.id}" name="checkbox" type="checkbox">
                                                                <label for="checkbox-${item_detail.id}">${item_detail.menuName}</label>
                                                            </div>
                                                        </#if>
                                                    </#list>
                                                </div>
                                            </div>
                                        </#if>
                                    </#list>
                                </div>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                    </form>
                </div>
                <div class="ibox-footer overflow">
                    <button type="button" onclick="javascript:window.location.href=history.go(-1)" class="btn btn-default pull-left">取消修改</button>
                    <button type="button" id="user_edit" class="btn btn-primary pull-right">确认修改</button>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    $(document).ready(function () {
        $('.i-checks').iCheck({
            checkboxClass: 'icheckbox_square-green',
            radioClass: 'iradio_square-green'
        });

        //全选
        $(".is-checkedAll").click(function () {
            var $subBox = $(this).parents(".is-checkbox").find("input[name='checkbox']");
            if (!this.checked) {
                for (var i = 0; i < $subBox.length; i++) {
                    $subBox[i].checked = false;
                }
            } else {
                for (var j = 0; j < $subBox.length; j++) {
                    $subBox[j].checked = true;
                }
            }
        });
        
        // 显示门店 初始化
        var storeId = $("#storeId option:selected").val();
        debugger;
        if(storeId > 0){
        	$('#private_show').show(400);
        }else{
        	$('#private_show').hide(400);
        }
        // 门店事件
         // 用户类型事件
        $("#roleId").change(function(){
        	// 当选择餐饮管理员的时候就显示门店列表提供选择
        	var roleId = $(this).val();
        	if(roleId == 3){
        		$('#private_show').show(400);
        	}else{
        		$('#private_show').hide(400);
        		$('#storeId').val(0);
        	}
        
        });
        
        $("#user_edit").click(function () {
            var userName = $("#userName").val();
            var mobile = $("#mobile").val();
            if (userName == '') {
                swal("用户姓名不能为空!",'','error');
                $("#userName").focus();
                return;
            }
            if (mobile == '') {
                swal("手机不能为空!",'','error');
                $("#mobile").focus();
                return;
            }
            var roleId = $("#roleId option:selected").val();
            if (roleId == '') {
                swal("请选择用户类型!",'','error');
                $("#roleId").focus();
                return;
            }
            var menusId = "";
            $('input[type="checkbox"]:checked').each(function () {
                menusId += $(this).val() + ",";
            });
            $("#menusId").val(menusId);
            var url = "user_edit";
            var data = $("form").serialize();
			
            var storeId = $("#storeId option:selected").val();
            debugger;
            if ((roleId == 3 || roleId == '3' ) && (storeId == '' || storeId == 0 || storeId == '0')) {
                swal("请选择门店!",'','error');
                $("#roleId").focus();
                return;
            }
            
            swal({
                title: '确认修改',
                text: "",
                type: 'question',
                showCancelButton: true,
                cancelButtonColor: '#d33',
                confirmButtonText: 'Yes, 确认修改',
                cancelButtonText: 'No, 再次确认'
            }).then(function () {
                $.post(url,data,function (result) {
                    if (result > 0) {
                        swal("修改成功!",'页面即将跳转,请稍后...','success');
                        window.setTimeout(function () {
                            window.history.go(-1);
                        }, 1000)
                    } else if (result == -1) {
                        swal("手机号码已被注册!",'','info');
                        $("#mobile").focus();
                    } else {
                        swal(returndata,'','error');
                    }
                });
//                $.ajax({
//                    url: url,
//                    type: 'POST',
//                    data: data,
//                    async: false,
//                    success: function (result) {
//                        if (result > 0) {
//                            swal("修改成功!",'页面即将跳转,请稍后...','success');
//                            window.setTimeout(function () {
//                                window.history.go(-1);
//                            }, 1000)
//                        } else if (result == -1) {
//                            swal("手机号码已被注册!",'','info');
//                            $("#mobile").focus();
//                        }
//                    },
//                    error: function (returndata) {
//                        swal(returndata,'','error');
//                    }
//                });
            });
        });
    });

    function Trim(str, is_global) {
        var result;
        result = str.replace(/(^\s+)|(\s+$)/g, "");
        if (is_global.toLowerCase() == "g")
            result = result.replace(/\s/g, "");
        return result;
    }
</script>

</@body.page>

</html>